package com.jjh.common.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jjh.common.pojo.InfoEnum;
import com.jjh.common.util.VerifyCodeUtils;

/**
 * 公共的Servlet(功能公共)
 * @author Zjx
 *
 */
@Controller
@RequestMapping("/common")
public class CommonServlet extends BaseServlet
{

	/**
	 * 验证码
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/randImg")
	private void randImg(HttpServletRequest request, HttpServletResponse response) throws IOException
	{
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		response.setContentType("image/jpeg");
		
		/**
		 * 随机数
		 */
		String ranCode = this.regexUtil.proccedRandStr(4, InfoEnum.VAR_NUMCHAR);
		
		//存入会话session
		HttpSession session = request.getSession();
		/* session中的键是randSess */
		session.setAttribute("randSess", ranCode);
		//生成图片
		int w = 250, h = 100;
		VerifyCodeUtils.outputImage(w, h, response.getOutputStream(), ranCode);
	}
}
